package com.mlz.mlzmall.ui.activity.act.personal;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.JsonRequest;
import com.google.gson.Gson;
import com.mlz.mlzmall.R;
import com.mlz.mlzmall.bean.GetPointHistoryResponse;
import com.mlz.mlzmall.ui.activity.act.base.BaseActivity;
import com.mlz.mlzmall.ui.activity.act.main.MyApplication;
import com.mlz.mlzmall.ui.utils.mlz.ConfigInfo;
import com.mlz.mlzmall.ui.utils.mlz.URL;
import com.mlz.mlzmall.ui.utils.util.LogUtils;

import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/5/23.
 */
public class IntegralRecordActivity extends BaseActivity{
    private RelativeLayout iv_back_integration;
    private RelativeLayout rl_must_read_recoder;
    private List<GetPointHistoryResponse.PageableDto.PointHistoryDto> historypoints;
    private TextView tv_integration_total_point;
    private ListView listView;
    private RelativeLayout iv_back_integration_recorder;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_integralrecord);
        initView();
        getData();
    }

    private void initView() {
        listView = (ListView)findViewById(R.id.lv_integration_recoder);
        iv_back_integration = (RelativeLayout) findViewById(R.id.iv_back_integration_recorder);
        rl_must_read_recoder= (RelativeLayout)findViewById(R.id.rl_must_read_recoder);
        iv_back_integration_recorder = (RelativeLayout)findViewById(R.id.iv_back_integration_recorder);
        iv_back_integration_recorder.setOnClickListener(this);
        rl_must_read_recoder.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId())
        {
            case R.id.iv_back_integration_recorder:
                finish();
            //    overridePendingTransition(R.anim.left_out,R.anim.right_in);
                break;
            case R.id.rl_must_read_recoder:
                break;
        }
    }

    public void getData() {
        JsonRequest jsonRequest = new JsonObjectRequest(Request.Method.GET, URL.POINT.LIST, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                LogUtils.e(jsonObject.toString());
                Gson gson = new Gson();
                GetPointHistoryResponse bean = gson.fromJson(jsonObject.toString(),GetPointHistoryResponse.class);
                historypoints = bean.getData().getList();
                listView.setAdapter(new MyAdapter());
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                volleyError.printStackTrace();
            }
        }){
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put(ConfigInfo.CHARSET, ConfigInfo.UTF8);
                map.put(ConfigInfo.COOKIE, ConfigInfo.COOKIE_KEY + sessionId);
                map.put(ConfigInfo.TYPE, ConfigInfo.TYPE_KEY);
                return map;
            }
        };
        // 设置该请求的标签
        jsonRequest.setTag(tag);
        // 将请求添加到队列中
        ((MyApplication) getApplication()).getRequestQueue().add(jsonRequest);
    }

    private class MyAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return historypoints.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_integralrecord, null);
            ViewHolder vh = new ViewHolder();
            vh.reason = (TextView) convertView.findViewById(R.id.tv_Integral_details);
            vh.dateAdded = (TextView)convertView.findViewById(R.id.integration_time);
            vh.pointNum = (TextView)convertView.findViewById(R.id.tv_point_integration);

            if (convertView != null) {

                vh.reason.setText(historypoints.get(position).getReason());//显示积分详情
                vh.pointNum.setText(String.valueOf(historypoints.get(position).getPointNum()));//显示积分分数
                //将后台的long型开始时间转换为正常日期并显示    显示时间
                Date dat_start = new Date(historypoints.get(position).getDateAdded());
                java.util.GregorianCalendar gc = new java.util.GregorianCalendar();
                gc.setTime(dat_start);
                SimpleDateFormat formatstart =new SimpleDateFormat("yyyy-MM-dd");
                String sb = formatstart.format(gc.getTime());
                vh.dateAdded.setText(sb);
            }

            return convertView;
        }
        class ViewHolder {
            TextView reason;
            TextView dateAdded;
            TextView pointNum;
        }
    }
}
